import com.farm.AgriculturalMachine;
import com.farm.chain.*;
import com.farm.command.*;
import com.farm.decorator.AutoPilotDecorator;
import com.farm.decorator.GPSDecorator;
import com.farm.decorator.SoilAnalysisDecorator;
import com.farm.facade.AgriculturalSystemFacade;
import com.farm.factory.MachineFactory;
import com.farm.factory.SeedingMachineFactory;
import com.farm.observer.AgriculturalMachineObserver;
import com.farm.observer.EnvironmentSensor;
import com.farm.observer.Observer;
import com.farm.state.AgriculturalMachineContext;
import com.farm.state.WorkingState;
import com.farm.strategy.AvoidObstacleStrategy;
import com.farm.strategy.PathPlanner;
import com.farm.strategy.PathPlanningStrategy;
import com.farm.strategy.WetSoilStrategy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AgriculturalSystemDemo {
    public static void main(String[] args) {
        System.out.println("===== 双播种机多线程工作演示 =====");

        // 创建线程池(2个线程对应2台播种机)
        ExecutorService executor = Executors.newFixedThreadPool(2);

        // 启动第一台播种机
        executor.execute(() -> runAgriculturalMachine("播种机-A1"));

        // 启动第二台播种机
        executor.execute(() -> runAgriculturalMachine("播种机-B2"));


        // 关闭线程池
        executor.shutdown();
    }

    public static void runAgriculturalMachine(String machineName) {
        // 1. 使用工厂方法创建农机
        MachineFactory factory = new SeedingMachineFactory();
        System.out.println(machineName + ": 正在通过工厂创建农机...");
        AgriculturalMachine seeder = factory.createMachine();

        // 2. 使用装饰器添加功能
        System.out.println(machineName + ": 正在为农机添加GPS、自动驾驶和土壤分析功能...");
        AgriculturalMachine decoratedSeeder = new GPSDecorator(
                new AutoPilotDecorator(
                        new SoilAnalysisDecorator(seeder)
                )
        );

        // 3. 创建环境传感器(观察者模式)
        System.out.println(machineName + ": 创建环境传感器...");
        EnvironmentSensor sensor = new EnvironmentSensor();
        Observer machineObserver = new AgriculturalMachineObserver(machineName);
        sensor.registerObserver(machineObserver);

        // 6. 创建命令
        AgriculturalMachineReceiver receiver = new AgriculturalMachineReceiver(machineName);
        Command startCommand = new StartCommand(receiver);

        //发出启动命令(4.在启动命令里会执行状态模式的工作状态代码)
        System.out.println(machineName +"--- 发送控制命令 ---");
        RemoteControlInvoker remote = new RemoteControlInvoker();
        remote.setCommand(startCommand);
        remote.pressButton();



        // 5. 创建责任链处理紧急事件
        EmergencyHandler handlerChain = new ObstacleHandler();
        handlerChain.setNextHandler(new LowFuelHandler());
        handlerChain.setNextHandler(new SystemFaultHandler());


        // 7. 使用外观模式简化操作
        AgriculturalSystemFacade facade = new AgriculturalSystemFacade();
        facade.startDailyOperation(machineName);

        // 模拟系统运行
        System.out.println(machineName + "--- 运行中 ---");
        decoratedSeeder.operate(machineName);

        // 模拟环境变化（当土壤湿度不符合要求时观察者会调整策略-策略模式）
        System.out.println(machineName +"--- 环境变化 ---");
        float soilMoisture = (float)(Math.random() * 100);
        sensor.setMeasurements(
                20 + (float)(Math.random() * 10),
                50 + (float)(Math.random() * 20),
                soilMoisture
        );


        // 模拟紧急事件，当检测到障碍物时，使用策略模式调整路径
        EmergencyRequest  request = new EmergencyRequest("OBSTACLE", "前方检测到障碍物");
        handlerChain.handleRequest(request, machineName);


        // 创建并发送停止命令（状态转为空闲状态）
        Command stopCommand = new StopCommand(receiver);
        System.out.println(machineName +"--- 发送控制命令 ---");
        remote.setCommand(stopCommand);
        remote.pressButton();

        System.out.println(machineName +"===== 运行结束 =====");

    }
}